package com.example.springboot.controller;

import com.example.springboot.bean.Department;
import com.example.springboot.config.anotation.requireduser.Required;
import com.example.springboot.config.mysql.UseDataSource;
import com.example.springboot.mapper.DepartmentMapper;
import com.example.springboot.service.DeptService;
import com.example.springboot.utils.ApplicationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/10/9 20:59
 */
@RestController
public class DeptController {
    @Autowired
    DeptService deptService;

    @Required
    @GetMapping("/dept/{id}")
    public Department getDepartment(@PathVariable("id") Integer id) {
        return deptService.getDeptById(id);
    }

    @GetMapping("/deptt/{id}")
    public Department getDepartment_1(@PathVariable("id") Integer id) {
        return deptService.getDeptById_1(id);
    }

    @GetMapping("/dept")
    public Department insert(Department department) {
        deptService.insertDept(department);
        return department;
    }

    /**
     * 容器上下文持有对象的测试
     */
    @GetMapping("/dept/test")
    @UseDataSource("alibaba")
    public Department get() {
        // 拿到对象
        DepartmentMapper departmentMapper = ApplicationUtils.get();
        // 打印字节码
        System.out.println(departmentMapper.getClass());
        Department department = departmentMapper.getById(1);
        System.out.println(department);
        return department;
    }
}
